package com.qiangesoft.mybatisplusjoin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.DeleteJoinWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.github.yulichang.wrapper.UpdateJoinWrapper;
import com.qiangesoft.mybatisplusjoin.entity.SysDept;
import com.qiangesoft.mybatisplusjoin.entity.SysUser;
import com.qiangesoft.mybatisplusjoin.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @since 2024-04-11
 */
@RestController
@RequestMapping("/sys-user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/page")
    public Page<SysUser> page() {
        // SELECT t.*,t1.dept_name FROM sys_user t LEFT JOIN sys_dept t1 ON t1.id = t.dept_id WHERE (t.id = ?) LIMIT ?,?
        MPJLambdaWrapper<SysUser> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(SysUser.class)
                .select(SysDept::getDeptName)
                .leftJoin(SysDept.class, SysDept::getId, SysUser::getDeptId)
                .eq(SysUser::getId, 1);
        return sysUserService.selectJoinListPage(new Page<>(1, 10), SysUser.class, wrapper);
    }

    @GetMapping("/list")
    public List<SysUser> list() {
        // SELECT t.*,t1.dept_name FROM sys_user t LEFT JOIN sys_dept t1 ON t1.id = t.dept_id WHERE (t.id = ?)
        MPJLambdaWrapper<SysUser> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(SysUser.class)
                .select(SysDept::getDeptName)
                .leftJoin(SysDept.class, SysDept::getId, SysUser::getDeptId)
                .eq(SysUser::getId, 1);
        return sysUserService.selectJoinList(SysUser.class, wrapper);
    }

    @GetMapping("/deleteJoinMaster")
    public Boolean deleteJoinMaster() {
        // DELETE t FROM `sys_user` t LEFT JOIN sys_dept t1 ON (t1.id = t.dept_id) WHERE (t.id = ?)
        DeleteJoinWrapper<SysUser> wrapper = JoinWrappers.delete(SysUser.class)
                .leftJoin(SysDept.class, SysDept::getId, SysUser::getDeptId)
                .eq(SysUser::getId, 1);
        return sysUserService.deleteJoin(wrapper);
    }

    @GetMapping("/deleteJoinAll")
    public Boolean deleteJoinAll() {
        // DELETE t,t1 FROM `sys_user` t LEFT JOIN sys_dept t1 ON (t1.id = t.dept_id) WHERE (t.id = ?)
        DeleteJoinWrapper<SysUser> wrapper = JoinWrappers.delete(SysUser.class)
                .deleteAll()
                .leftJoin(SysDept.class, SysDept::getId, SysUser::getDeptId)
                .eq(SysUser::getId, 1);
        return sysUserService.deleteJoin(wrapper);
    }

    @GetMapping("/update")
    public Boolean update() {
        // UPDATE `sys_user` t LEFT JOIN sys_dept t1 ON (t1.id = t.dept_id) SET t.nick_name=?, t1.dept_name=? WHERE (t.id = ?)
        UpdateJoinWrapper<SysUser> wrapper = JoinWrappers.update(SysUser.class)
                .set(SysUser::getNickName, "aaaaaa")
                .set(SysDept::getDeptName, "bbbbb")
                .leftJoin(SysDept.class, SysDept::getId, SysUser::getDeptId)
                .eq(SysUser::getId, 1);
        return sysUserService.updateJoin(null, wrapper);
    }
}

